package iotwisp.school.wx.entity;

import iotwisp.base.orm.StringIdBaseEntity;
import iotwisp.base.util.UUIDGenerator;
import iotwisp.school.wx.dto.WxGetAccessTokenResponse;
import iotwisp.school.user.entity.User;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;

import javax.persistence.*;
import java.util.Calendar;
import java.util.Date;

@SuppressWarnings("serial")
@Data
@EqualsAndHashCode(callSuper = false)
@DynamicUpdate
@DynamicInsert
@Entity
@Table(name = M.TABLE_PREFIX + "wx_user")
public class WxUser extends StringIdBaseEntity {

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "user_id", foreignKey = @ForeignKey(name = "none", value = ConstraintMode.NO_CONSTRAINT))
    private User user;

    @Column(name = "access_token", length = 64)
    private String accessToken;
    @Column(name = "access_token_expire_date", columnDefinition = M.DATE_COLUMN)
    private Date accessTokenExpireDate;
    @Column(name = "refresh_token", length = 64)
    private String refreshToken;
    @Column(name = "refresh_token_expire_date", columnDefinition = M.DATE_COLUMN)
    private Date refreshTokenExpireDate;
    @Column(name = "open_id", length = 32)
    private String openId;
    @Column(name = "scope", length = 256)
    private String scope;
    @Column(name = "union_id", length = 64)
    private String unionId;
    @Column(name = "create_date", columnDefinition = M.DATE_COLUMN)
    private Date createDate;

    public void create(WxGetAccessTokenResponse response) {
        setId(UUIDGenerator.getUUID());

        setAccessToken(response.getAccess_token());

        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.SECOND, response.getExpires_in() - 10);
        setAccessTokenExpireDate(calendar.getTime());

        setRefreshToken(response.getRefresh_token());

        Calendar calendar2 = Calendar.getInstance();
        calendar2.add(Calendar.DATE, 30);
        setRefreshTokenExpireDate(calendar2.getTime());

        setOpenId(response.getOpenid());
        setScope(response.getScope());
        setUnionId(response.getUnionid());

        setCreateDate(new Date());
    }

    public void refresh(WxGetAccessTokenResponse response) {

        setAccessToken(response.getAccess_token());

        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.SECOND, response.getExpires_in() - 10);
        setAccessTokenExpireDate(calendar.getTime());

        if (StringUtils.equals(response.getRefresh_token(), getRefreshToken())) {
            setRefreshToken(response.getRefresh_token());

            Calendar calendar2 = Calendar.getInstance();
            calendar2.add(Calendar.DATE, 30);
            setRefreshTokenExpireDate(calendar2.getTime());
        }

        setOpenId(response.getOpenid());
        setScope(response.getScope());
        setUnionId(response.getUnionid());

    }
}
